/*
 * BottomWidget.cpp
 *
 *  Created on: 2009-02-12
 *      Author: ck
 */

#include "BottomWidget.h"

BottomWidget::BottomWidget(QWidget * parent) :
	QWidget(parent) {
	QVBoxLayout * vLayout = new QVBoxLayout;
	QHBoxLayout * hLayout = new QHBoxLayout;
	QHBoxLayout * h2Layout = new QHBoxLayout;

	btnConnect = new QPushButton("Connect");

	btnReady = new QPushButton("Sit");
	btnReady->setEnabled(false);
	btnReady->setFixedWidth(60);

	btnLeave = new QPushButton("Leave");
	btnLeave->setEnabled(false);
	btnLeave->setFixedWidth(60);

	edHost = new QLineEdit("localhost");
	edHost->setMinimumWidth(120);

	edPort = new QLineEdit("2012");
	edPort->setMinimumWidth(60);

	lblStatus = new QLabel("Disconnected");

	lblTableStatus = new QLabel("");
	lblTableStatus->setFixedHeight(15);
	lblTableStatus->setAlignment(Qt::AlignCenter);

	hLayout->addWidget(btnConnect);
	hLayout->addWidget(edHost);
	hLayout->addWidget(edPort);
	hLayout->addWidget(btnReady);

	//vLayout->setAlignment(gameField, Qt::AlignCenter);
	vLayout->addWidget(lblStatus);
	vLayout->addLayout(hLayout);
	vLayout->setAlignment(lblStatus, Qt::AlignCenter);

	h2Layout->addWidget(lblTableStatus);
	h2Layout->addWidget(btnLeave);
	h2Layout->setAlignment(lblTableStatus, Qt::AlignCenter);
	//h2Layout->setAlignment(btnLeave, Qt::AlignRight);
	vLayout->addLayout(h2Layout);

	setLayout(vLayout);

	connect(btnConnect, SIGNAL(clicked()), this, SLOT(onConnectClicked()));
	connect(btnReady, SIGNAL(clicked()), this, SIGNAL(readyClicked()));
	connect(btnLeave, SIGNAL(clicked()), this, SIGNAL(leaveClicked()));
}

void BottomWidget::onConnStateChanged(QAbstractSocket::SocketState socketState) {
	switch (socketState) {
		case QAbstractSocket::UnconnectedState:
			lblStatus->setText("Disconnected");
			lblTableStatus->setText("");
			btnConnect->setText("Connect");
			btnReady->setText("Sit");
			edHost->setEnabled(true);
			edPort->setEnabled(true);
			btnLeave->setEnabled(false);
			btnReady->setEnabled(false);
			break;
		case QAbstractSocket::HostLookupState:
			btnConnect->setText("Disconn.");
			lblStatus->setText("Host Lookup");
			edHost->setEnabled(false);
			edPort->setEnabled(false);
			btnLeave->setEnabled(false);
			btnReady->setEnabled(false);
			break;
		case QAbstractSocket::ConnectingState:
			btnConnect->setText("Disconn.");
			lblStatus->setText("Host Lookup");
			edHost->setEnabled(false);
			edPort->setEnabled(false);
			btnLeave->setEnabled(false);
			btnReady->setEnabled(false);
			break;
		case QAbstractSocket::ConnectedState:
			lblStatus->setText("Connected");
			btnConnect->setText("Disconn.");
			edHost->setEnabled(false);
			edPort->setEnabled(false);
			btnLeave->setEnabled(false);
			btnReady->setEnabled(true);
			break;
		case QAbstractSocket::BoundState:
			lblStatus->setText("Bound");
			break;
		case QAbstractSocket::ClosingState:
			lblStatus->setText("Closing");
			break;
		case QAbstractSocket::ListeningState:
			lblStatus->setText("Listening");
			break;
		default:
			break;
		}
}

void BottomWidget::onSit() {
	btnReady->setText("Ready");
	btnLeave->setEnabled(true);
}

void BottomWidget::onLeave() {
	btnReady->setEnabled(true);
	btnReady->setText("Sit");
	btnLeave->setEnabled(false);
}

void BottomWidget::onReady() {
	btnReady->setEnabled(false);
	btnReady->setText("Ready");
	btnLeave->setEnabled(true);
}

void BottomWidget::onConnectClicked() {
	emit connectClicked(edHost->text() ,edPort->text());
}

BottomWidget::~BottomWidget() {
	// TODO Auto-generated destructor stub
}
